<template>
  <view class="my-teaching">
    <!-- 页面标题 -->
    <view class="header">
      <text class="title">我的授课</text>
    </view>
    
    <!-- 状态切换 -->
    <view class="status-tabs">
      <view 
        v-for="(tab, index) in tabs" 
        :key="index"
        :class="['tab-item', currentTab === tab.value ? 'active' : '']"
        @tap="switchTab(tab.value)"
      >
        {{ tab.name }}
      </view>
    </view>
    
    <!-- 加载状态 -->
    <view class="loading-container" v-if="isLoading">
      <view class="loading-spinner"></view>
      <text class="loading-text">加载中...</text>
    </view>
    
    <!-- 学生列表 -->
    <view class="students-list" v-else-if="studentsList.length > 0">
      <view class="student-item" v-for="(student, index) in studentsList" :key="index">
        <view class="student-info">
          <view class="student-header">
            <view class="student-avatar-name">
              <image class="student-avatar" :src="student.avatarUrl" mode="aspectFill"></image>
              <view class="student-basic">
                <text class="student-name">{{ student.studentName }}</text>
                <text class="student-phone">{{ student.studentPhone }}</text>
              </view>
            </view>
            <text :class="['course-status', getStatusClass(student.status)]">{{ getStatusText(student.status) }}</text>
          </view>
          
          <view class="course-details">
            <view class="detail-row">
              <text class="label">课程：</text>
              <text class="value">{{ student.courseTitle }}</text>
            </view>
            <view class="detail-row">
              <text class="label">年级：</text>
              <text class="value">{{ student.grade }}</text>
            </view>
            <view class="detail-row">
              <text class="label">预约日期：</text>
              <text class="value">{{ student.bookingDate }}</text>
            </view>
            <view class="detail-row">
              <text class="label">时间段：</text>
              <text class="value">{{ student.timeSlot }}</text>
            </view>
            <view class="detail-row">
              <text class="label">地址：</text>
              <text class="value">{{ student.province }} {{ student.city }} {{ student.district }}</text>
            </view>
            <view class="detail-row" v-if="student.detailAddress">
              <text class="label">详细地址：</text>
              <text class="value">{{ student.detailAddress }}</text>
            </view>
            <view class="detail-row">
              <text class="label">价格：</text>
              <text class="value price">¥{{ student.price }}</text>
            </view>
          </view>
        </view>
        
        <!-- 操作按钮 -->
        <view class="course-actions">
          <button class="action-btn contact" @tap="contactStudent(student)">联系学生</button>
          <button class="action-btn complete" @tap="completeCourse(student)" v-if="student.status === 1">完成授课</button>
        </view>
      </view>
    </view>
    
    <!-- 空状态 -->
    <view class="empty-state" v-else>
      <image class="empty-icon" src="/static/empty.png" mode="aspectFit"></image>
      <text class="empty-text">暂无授课记录</text>
    </view>
    
    <!-- 完成授课确认弹窗 -->
    <view class="modal" v-if="showCompleteModal">
      <view class="modal-content">
        <view class="modal-title">确认完成</view>
        <view class="modal-message">是否确认已完成该课程的授课？</view>
        <view class="modal-actions">
          <button class="modal-btn cancel" @tap="showCompleteModal = false">取消</button>
          <button class="modal-btn confirm" @tap="confirmComplete">确认</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { teacherApi } from '../../api'

export default {
  data() {
    return {
      isLoading: true,
      currentTab: 'all',
      tabs: [
        { name: '全部', value: 'all' },
        { name: '进行中', value: '1' },
        { name: '已完成', value: '3' }
      ],
      studentsList: [],
      originalList: [],
      showCompleteModal: false,
      currentStudent: null
    }
  },
  onLoad() {
    this.loadStudents()
  },
  onShow() {
    this.loadStudents()
  },
  onPullDownRefresh() {
    this.loadStudents()
  },
  methods: {
    // 获取教师学生列表
    async loadStudents() {
      try {
        this.isLoading = true
        
        // 获取用户信息
        const userInfo = uni.getStorageSync('userInfo')
        if (!userInfo || !userInfo.id) {
          uni.showToast({
            title: '请先登录',
            icon: 'none'
          })
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages/login/login'
            })
          }, 1500)
          return
        }
        
        // 请求教师学生列表
        const res = await teacherApi.getTeachersStudent(userInfo.id)
        
        console.log('获取学生列表结果:', res)
        
        if (res.code === 200) {
          this.originalList = res.data || []
          this.filterStudents()
        } else {
          uni.showToast({
            title: res.message || '获取数据失败',
            icon: 'none'
          })
          this.studentsList = []
        }
      } catch (error) {
        console.error('获取学生列表失败:', error)
        uni.showToast({
          title: '网络错误，请稍后重试',
          icon: 'none'
        })
        this.studentsList = []
      } finally {
        this.isLoading = false
        uni.stopPullDownRefresh()
      }
    },

    // 切换状态标签
    switchTab(tabValue) {
      this.currentTab = tabValue
      this.filterStudents()
    },

    // 根据状态过滤学生
    filterStudents() {
      if (this.currentTab === 'all') {
        this.studentsList = this.originalList
      } else {
        this.studentsList = this.originalList.filter(student => 
          student.status.toString() === this.currentTab
        )
      }
    },

    // 获取状态文本
    getStatusText(status) {
      switch (status) {
        case 1:
          return '进行中'
        case 3:
          return '已完成'
        default:
          return '未知状态'
      }
    },

    // 获取状态样式类
    getStatusClass(status) {
      switch (status) {
        case 1:
          return 'ongoing'
        case 3:
          return 'completed'
        default:
          return 'unknown'
      }
    },

    // 联系学生
    contactStudent(student) {
      uni.makePhoneCall({
        phoneNumber: student.studentPhone,
        fail: () => {
          uni.showToast({
            title: '拨打电话失败',
            icon: 'none'
          })
        }
      })
    },

    // 完成授课
    completeCourse(student) {
      this.currentStudent = student
      this.showCompleteModal = true
    },

    // 确认完成授课
    async confirmComplete() {
      try {
        this.showCompleteModal = false
        
        uni.showLoading({
          title: '处理中...'
        })

        const res = await teacherApi.completeCourse({
          bookingId: parseInt(this.currentStudent.id)
        })

        uni.hideLoading()

        if (res.code === 200) {
          uni.showToast({
            title: '完成授课成功',
            icon: 'success'
          })
          // 重新加载数据
          this.loadStudents()
        } else {
          uni.showToast({
            title: res.message || '操作失败',
            icon: 'none'
          })
        }
      } catch (error) {
        uni.hideLoading()
        console.error('完成授课失败:', error)
        uni.showToast({
          title: '网络错误，请稍后重试',
          icon: 'none'
        })
      }
    }
  }
}
</script>

<style>
.my-teaching {
  min-height: 100vh;
  background: #f5f6fa;
}

.header {
  background: #fff;
  padding: 30rpx;
  text-align: center;
  border-bottom: 1rpx solid #eee;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.status-tabs {
  display: flex;
  background: #fff;
  margin-bottom: 20rpx;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 30rpx 0;
  font-size: 28rpx;
  color: #666;
  border-bottom: 4rpx solid transparent;
  transition: all 0.3s;
}

.tab-item.active {
  color: #4080FF;
  border-bottom-color: #4080FF;
  font-weight: bold;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
}

.loading-spinner {
  width: 60rpx;
  height: 60rpx;
  border: 4rpx solid #f3f3f3;
  border-top: 4rpx solid #4080FF;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 20rpx;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  font-size: 28rpx;
  color: #999;
}

.students-list {
  padding: 0 20rpx;
}

.student-item {
  background: #fff;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
}

.student-info {
  padding: 30rpx;
}

.student-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.student-avatar-name {
  display: flex;
  align-items: center;
}

.student-avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 40rpx;
  margin-right: 20rpx;
}

.student-basic {
  display: flex;
  flex-direction: column;
}

.student-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
}

.student-phone {
  font-size: 26rpx;
  color: #666;
}

.course-status {
  font-size: 24rpx;
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-weight: bold;
}

.course-status.ongoing {
  background: #e8f4fd;
  color: #4080FF;
}

.course-status.completed {
  background: #e8f5e8;
  color: #52c41a;
}

.course-status.unknown {
  background: #f5f5f5;
  color: #999;
}

.course-details {
  margin-top: 20rpx;
}

.detail-row {
  display: flex;
  margin-bottom: 12rpx;
  align-items: flex-start;
}

.detail-row:last-child {
  margin-bottom: 0;
}

.label {
  font-size: 28rpx;
  color: #666;
  width: 140rpx;
  flex-shrink: 0;
}

.value {
  font-size: 28rpx;
  color: #333;
  flex: 1;
  word-break: break-all;
}

.value.price {
  color: #ff6b6b;
  font-weight: bold;
}

.course-actions {
  display: flex;
  gap: 20rpx;
  padding: 20rpx 30rpx;
  background: #f8f9fa;
  border-top: 1rpx solid #eee;
}

.action-btn {
  flex: 1;
  height: 70rpx;
  line-height: 70rpx;
  text-align: center;
  border-radius: 35rpx;
  font-size: 28rpx;
  border: none;
  transition: all 0.3s;
}

.action-btn.contact {
  background: #4080FF;
  color: #fff;
}

.action-btn.contact:active {
  background: #3366cc;
}

.action-btn.complete {
  background: #52c41a;
  color: #fff;
}

.action-btn.complete:active {
  background: #389e0d;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 40rpx;
}

.empty-icon {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 30rpx;
  opacity: 0.6;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
  margin-bottom: 40rpx;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.modal-content {
  background: #fff;
  border-radius: 16rpx;
  padding: 40rpx;
  margin: 0 40rpx;
  max-width: 600rpx;
  width: 100%;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-bottom: 20rpx;
}

.modal-message {
  font-size: 28rpx;
  color: #666;
  text-align: center;
  margin-bottom: 40rpx;
  line-height: 1.5;
}

.modal-actions {
  display: flex;
  gap: 20rpx;
}

.modal-btn {
  flex: 1;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  border-radius: 40rpx;
  font-size: 28rpx;
  border: none;
}

.modal-btn.cancel {
  background: #f5f5f5;
  color: #666;
}

.modal-btn.confirm {
  background: #52c41a;
  color: #fff;
}

.modal-btn:active {
  opacity: 0.8;
}
</style>
